-
Notifications
You must be signed in to change notification settings - Fork 36
/
DictionaryOutboundRouter`2.cs
114 lines (105 loc) · 4.67 KB
/
DictionaryOutboundRouter`2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2020 Sergio Aquilini
// This code is licensed under MIT license (see LICENSE file for details)
using System.Collections.Generic;
using Silverback.Messaging.Messages;
using Silverback.Util;
namespace Silverback.Messaging.Outbound.Routing
{
/// <summary>
/// Routes the outbound messages to one or multiple endpoints.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the messages to be routed.
/// </typeparam>
/// <typeparam name="TEndpoint">
/// The type of the <see cref="IProducerEndpoint" />.
/// </typeparam>
public class DictionaryOutboundRouter<TMessage, TEndpoint> : OutboundRouter<TMessage>
where TEndpoint : IProducerEndpoint
{
private readonly IReadOnlyDictionary<string, TEndpoint> _endpoints;
private readonly RouterFunction _routerFunction;
/// <summary>
/// Initializes a new instance of the <see cref="DictionaryOutboundRouter{TMessage, TEndpoint}" /> class.
/// </summary>
/// <param name="routerFunction">
/// The <see cref="SingleEndpointRouterFunction" />.
/// </param>
/// <param name="endpoints">
/// The <see cref="IReadOnlyDictionary{TKey,TValue}" /> containing the endpoints and their key.
/// </param>
public DictionaryOutboundRouter(
SingleEndpointRouterFunction routerFunction,
IReadOnlyDictionary<string, TEndpoint> endpoints)
: this(
(message, headers, endpointsDictionary) =>
new[] { routerFunction.Invoke(message, headers, endpointsDictionary) },
endpoints)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DictionaryOutboundRouter{TMessage, TEndpoint}" /> class.
/// </summary>
/// <param name="routerFunction">
/// The <see cref="RouterFunction" />.
/// </param>
/// <param name="endpoints">
/// The <see cref="IReadOnlyDictionary{TKey,TValue}" /> containing the endpoints and their key.
/// </param>
public DictionaryOutboundRouter(
RouterFunction routerFunction,
IReadOnlyDictionary<string, TEndpoint> endpoints)
{
_routerFunction = Check.NotNull(routerFunction, nameof(routerFunction));
_endpoints = Check.NotNull(endpoints, nameof(endpoints));
}
/// <summary>
/// The actual router method that receives the message (including its headers) together with the
/// dictionary containing all endpoints and returns the destination endpoints.
/// </summary>
/// <param name="message">
/// The message to be routed.
/// </param>
/// <param name="headers">
/// The message headers.
/// </param>
/// <param name="endpoints">
/// The dictionary containing all configured endpoints for this router.
/// </param>
/// <returns>
/// The destination endpoints.
/// </returns>
public delegate IEnumerable<TEndpoint> RouterFunction(
TMessage message,
MessageHeaderCollection headers,
IReadOnlyDictionary<string, TEndpoint> endpoints);
/// <summary>
/// The actual router method that receives the message (including its headers) together with the
/// dictionary containing all endpoints and returns the destination endpoint.
/// </summary>
/// <param name="message">
/// The message to be routed.
/// </param>
/// <param name="headers">
/// The message headers.
/// </param>
/// <param name="endpoints">
/// The dictionary containing all configured endpoints for this router.
/// </param>
/// <returns>
/// The destination endpoint.
/// </returns>
public delegate TEndpoint SingleEndpointRouterFunction(
TMessage message,
MessageHeaderCollection headers,
IReadOnlyDictionary<string, TEndpoint> endpoints);
/// <inheritdoc cref="IOutboundRouter.Endpoints" />
public override IEnumerable<IProducerEndpoint> Endpoints =>
(IEnumerable<IProducerEndpoint>)_endpoints.Values;
/// <inheritdoc cref="IOutboundRouter{TMessage}.GetDestinationEndpoints(TMessage,MessageHeaderCollection)" />
public override IEnumerable<IProducerEndpoint> GetDestinationEndpoints(
TMessage message,
MessageHeaderCollection headers) =>
(IEnumerable<IProducerEndpoint>)_routerFunction.Invoke(message, headers, _endpoints);
}
}